<?php
/**
 * Name: 上传附件类
 * User: Small_K
 * Date: 2018/1/11
 * Time: 16:08
 */

namespace App\Services\Service;

use App\Models\Attachment;
use Auth;

class UploadService
{
    /**
     *
     * 查询列表
     *
     * @param array|null $_options
     * @param int $limit
     * @param Attachment Models 是一个模型来的
     * @return mixed
     */
    public static function index(array $_options = null, $limit = 32)
    {
        $category = Attachment::whereIn('extension', $_options)->orderBy('id', 'DESC')->paginate($limit);
        return $category;
    }

    /**
     * 新增附件
     *
     * @param array|null $_options
     * @return Attachment
     */
    public static function store(array $_options = null)
    {
        $attachment = new Attachment();
        $attachment->name = $_options['name'];  //文件全称
        $attachment->file_name = $_options['file_name'];  //文件名
        $attachment->path = $_options['path'];  //路径
        $attachment->extension = $_options['extension'];    //类型
        $attachment->size = $_options['size'];    //大小
        $attachment->status = $_options['status'];    //状态
        $attachment->admin_id = isset($_options['user_id']) ? 0 : Auth::user()->id;    //管理员ID
        $res = $attachment->save();
        return $attachment;
    }
}